package com.lenovo.newsapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.model.HomeNews;
import com.lenovo.newsapp.util.Const;
import com.lenovo.newsapp.util.Tools;

public class NewsListAdapter extends BaseArrayAdapter<HomeNews> {

	private static final String TAG = NewsListAdapter.class.getSimpleName();

	private ViewHolder holder;

	private boolean isFirst;

	private HomeNews FirstItem;

	public NewsListAdapter(Context context, ArrayList<HomeNews> list) {
		super(context, list, null);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i(TAG, "getView");
		Log.i(TAG, "convertView = " + convertView);
		Log.i(TAG, "position = " + position);
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.home_list_item, null);
			holder = new ViewHolder();
			convertView.setTag(holder);
			holder.bigImg = (ImageView) convertView
					.findViewById(R.id.news_item_big_img);
			holder.title = (TextView) convertView
					.findViewById(R.id.news_item_title);
			holder.summary = (TextView) convertView
					.findViewById(R.id.news_item_summary);
			holder.img = (ImageView) convertView
					.findViewById(R.id.news_item_thumbnail);
			holder.bigTitle = (TextView) convertView
					.findViewById(R.id.news_item_big_title);
			holder.author = (TextView) convertView
					.findViewById(R.id.news_item_author);
			holder.time = (TextView) convertView
					.findViewById(R.id.news_item_time);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		HomeNews item = (HomeNews) getItem(position);
		holder.title.setText(item.getTitle());
		holder.summary.setText(item.getSummary());
		Log.v(TAG, "currentSection = "+Const.currentSection);
		if (Const.currentSection == Const.MAIL) {
			holder.img.setVisibility(View.GONE);
			holder.summary.setVisibility(View.GONE);
			holder.author.setVisibility(View.VISIBLE);
			holder.time.setVisibility(View.VISIBLE);
			holder.author.setText(item.getAuthor());
			holder.time.setText(item.getLastUpdateTime());
		} else {
			holder.img.setVisibility(View.VISIBLE);
			holder.img.setImageResource(R.drawable.empty);
			final String url = Const.DOMAIN + item.getNewsThumbnail();
			Tools.setImg(url, holder.img, context);
		}

		return convertView;
	}

	static class ViewHolder {
		TextView title;
		TextView summary;
		ImageView img;
		ImageView bigImg;
		TextView bigTitle;
		TextView author;
		TextView time;
	}

}
